package com.zhang.sparkstreaming_2

import com.alibaba.druid.pool.DruidDataSourceFactory

import java.sql.Connection
import java.util.Properties
import javax.sql.DataSource

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/22 19:10 
 */
object JDBCUtil {

    //初始化连接池
    var dataSource: DataSource = init()

    //初始化连接池方法
    def init(): DataSource = {
      val properties = new Properties()
      properties.setProperty("driverClassName", "com.mysql.jdbc.Driver")
      properties.setProperty("url", "jdbc:mysql://hadoop102:3306/spark?useUnicode=true&characterEncoding=UTF-8")
      properties.setProperty("username", "root")
      properties.setProperty("password", "000000")
      properties.setProperty("maxActive", "20")
      DruidDataSourceFactory.createDataSource(properties)
    }

    //获取MySQL连接
    def getConnection: Connection = {
      dataSource.getConnection
    }
}
